package com.ems.controller.supers.action;

import com.ems.pojo.data.ResultData;
import com.ems.pojo.data.RetunResult;
import com.ems.pojo.permission.Action;
import com.ems.pojo.permission.Group;
import com.ems.service.supers.action.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: 伟伟
 * @Date: 2019/10/5
 * @Description: 角色
 * @Version: 1.0
 */
@Controller
@CrossOrigin //允许所有ip跨域
@RequestMapping("/groupController")
public class GroupController {

    @Autowired
    @Qualifier("groupService")
    private GroupService groupService;
    /**
     * 获取所有角色信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAllGroup")
    public ResultData getAllGroup(){
        List<Group> list = groupService.getAllGroup();
        System.out.println(list);
        return RetunResult.AssemblyData(list);
    }

    /**
     * 添加角色
     * @return
     */
    @ResponseBody
    @RequestMapping("/addGroup")
    public ResultData addGroup(@RequestBody Group group){
        int result = groupService.addGroup(group);
        if(result > 0){
            return RetunResult.AssemblyData();
        }
        return RetunResult.AssemblyData("failure.to.add.roles");
    }

    /**
     * 修改角色
     */
    @ResponseBody
    @RequestMapping("/updateGroup")
    public ResultData updateGroup(@RequestBody Group group){
        if(groupService.updateGroup(group) > 0){
            return RetunResult.AssemblyData();
        }
        return RetunResult.AssemblyData("failure.to.update.roles");
    }

    /**
     * 删除角色
     */
    @ResponseBody
    @RequestMapping("/deleteGroup")
    public ResultData deleteGroup(@RequestBody Group group){
        int result = groupService.deleteGroup(group);
        if(result > 0){
            return RetunResult.AssemblyData();
        }else if(result == -1){
            return RetunResult.AssemblyData(300,"");
        }
        return RetunResult.AssemblyData("failure.to.delete.roles");
    }

    /**
     * 获取指定角色的权限树
     * @param group
     * @return
     */
    @ResponseBody
    @RequestMapping("/findActionTreeByGrId")
    public ResultData findActionTreeByGrId(@RequestBody Group group){
        List<Action> list = groupService.findActionTreeByGrId(group.getGr_id());
        return RetunResult.AssemblyData(list);
    }
}
